package com.exam.Controller;

import com.exam.Domain.User;
import com.exam.Service.TestService;
import com.exam.Service.UserService;
import com.exam.utils.MD5utils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @ClassName LoginController
 * @Description
 * @Author JY
 * @Date 2020/6/1 23:55
 **/
@Controller
public class LoginController {
    @Autowired
    private UserService userService;

    @Autowired
    private TestService testService;

    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    @RequestMapping("/register")
    public String register(){
        return "register";
    }

    @GetMapping("/detail")
    public String details(){
        return "detail";
    }

//检测账号信息错误处理问题
    @PostMapping("/loginChecked")
    public String loginChecked(User user, RedirectAttributes redirectAttributes, HttpSession session, Model model){
        User user1 = userService.findUserByName(user.getUserName());
        String Md5Str = MD5utils.encodeByMD5(user.getUserPassWord());
        if (user1==null){
            redirectAttributes.addFlashAttribute("message","查无此人");
            return "redirect:/login";
        }
       if (!Md5Str.equalsIgnoreCase(user1.getUserPassWord())){
           redirectAttributes.addFlashAttribute("message","密码不正确");
          return "redirect:/login";
       }
       if (Md5Str.equalsIgnoreCase(user1.getUserPassWord())&&user1.getUserName().equals(user.getUserName())){
           session.setAttribute("user",user1);
       }
        return "detail";
    }
//检测用户名重复问题
    @PostMapping("/check")
    @ResponseBody
    public boolean toVerifyUsername(HttpServletRequest request) {
        String userName = request.getParameter("userName");
        if (userService.VerifyUsername(userName)!=0){
            return false;
        }else {
            return true;
        }
    }

    @PostMapping("/registerUser")
    public String registerUser(HttpServletRequest request, Model model){
        User user = new User();
        user.setCreateTime(new Date());
        user.setUserName(request.getParameter("username"));
        user.setUserPassWord(MD5utils.encodeByMD5(request.getParameter("password")));
        userService.insertRegister(user);
        model.addAttribute("message","注册成功，可以登录了");
        return "redirect:/login";
    }

    @GetMapping("/aboutMe")
    public String aboutMe(){
        return "aboutMe";
    }

    @GetMapping("/loginOut")
    public String loginOut(HttpSession session){
        session.removeAttribute("user");
        return "redirect:/login";
    }

    @GetMapping("/updateMe")
    public void updateMe(HttpServletRequest req, HttpSession session, HttpServletResponse response) {
        String name = req.getParameter("name");
        String res = req.getParameter("toValue");
        User user =(User)session.getAttribute("user");
        String userId = user.getUserId();
        String toValue="";
        if (name.equals("password")){
             toValue = MD5utils.encodeByMD5(res);
        }else {
            toValue=res;
        }
        userService.updateMe(userId,name,toValue);
    }

}
